﻿#region Copyright Notice
// This file is part of GestionPedago.
// <description>
// Copyright (C) 2012 ENI Ecole Informatique
// 
// GestionPedago is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// <program> is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
#endregion

using System;
using System.Collections.Generic;
using System.Text;
using GestionPedago.EntityContext;
using GestionPedago.Services.Framework;
using GestionPedago.Services.ES.Modele;
using Persistence.OPManager;
using GestionPedago.Persistence.OPManager;

namespace GestionPedago.Services.Mapping
{
    /// <summary>
    /// Classe de mapping Entity<->ES de l'objet PlanningECF
    /// </summary>
    public class PlanningECFMappeur : IMappeur<PlanningECF, PlanningECFES>
    {
        /// <summary>
        /// Constructeur
        /// </summary>
        public PlanningECFMappeur() { }

        /// <summary>
        /// Mapping Entity -> ES
        /// </summary>
        /// <param name="entity">L'entity à mapper</param>
        /// <returns>L'ES obtenu</returns>
        public PlanningECFES Entity2ES(PlanningECF entity)
        {
            PlanningECFES retour = new PlanningECFES();
            retour.CodePlanningECF = entity.CodePlanningECF;
            retour.DateECF = entity.DateECF;
            retour.Duree = entity.Duree;
            retour.CodePromotion = entity.CodePromotion;
            retour.CodeContactENI = entity.CodeContactENI;
            if (entity.ContactENI != null)
            {
                retour.ContactENI = new ContactENIMappeur().Entity2ES(entity.ContactENI);
            }
            if (entity.ECF != null)
            {
                retour.ECF = new ECFMappeur().Entity2ES(entity.ECF);
                retour.IdECF = entity.ECF.IdECF;
            }
            else
            {
                retour.ECF = new ECFMappeur().Entity2ES(ECFOPManager.GetECFById(entity.IdECF));
            }
            /*retour.ListePlanningECFDetail = new List<PlanningECFDetailES>();
            foreach(PlanningECFDetail ped in entity.PlanningECFDetail) {
                retour.ListePlanningECFDetail.Add(new PlanningECFDetailMappeur().Entity2ES(ped));
            }*/
            if (entity.Promotion != null)
            {
                retour.Promotion = new PromotionMappeur().Entity2ES(entity.Promotion);
            }
            return retour;
        }

        /// <summary>
        /// Mapping ES -> Entity
        /// </summary>
        /// <param name="es">L'ES à mapper</param>
        /// <returns>L'entity obtenu</returns>
        public PlanningECF ES2Entity(PlanningECFES es)
        {
            PlanningECF retour = new PlanningECF();
            retour.CodePlanningECF = es.CodePlanningECF;
            if (retour.CodePlanningECF > 0)
            {
                retour = PlanningECFOPManager.GetPlanningECFByCode(es.CodePlanningECF);
            }
            bool alreadyAttached = !EntityStateChecker.IsDetached(retour) && !EntityStateChecker.IsAdded(retour);
            if (!alreadyAttached)
            {
                EntityConnection.GetContext().PlanningECF.Attach(retour);
            }
            retour.IdECF = es.IdECF;
            retour.DateECF = es.DateECF;
            retour.Duree = es.Duree;
            retour.CodePromotion = es.CodePromotion;
            retour.CodeContactENI = es.CodeContactENI;
            if(es.CodeContactENI != null)
            {
                retour.ContactENI = ContactENIOPManager.GetContactENIByCode(es.CodeContactENI.Value);
            }
            if(es.IdECF != 0) {
                retour.ECF = ECFOPManager.GetECFById(es.IdECF);
            }
            //TODO implémenter mapping Entity<->ES manuellement ! : PlanningECFDetail
            if (es.Promotion != null)
            {
                //retour.Promotion = new PromotionMappeur().ES2Entity(es.Promotion);
                //EntityConnection.GetContext().Titre.Attach(retour.Promotion.Formation.Titre);
                //retour.Promotion = PromotionOPManager.GetPromotionByCode(es.CodePromotion);
            }
            if (alreadyAttached)
            {
                EntityConnection.GetContext().PlanningECF.ApplyCurrentValues(retour);
            }
            return retour;
        }
    }
}

